package com.conversationboard.authenticator;

import java.io.IOException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Obfuscator {

	private static final int[] seed = new int[] { 12, 9, -11, 8, 24, -13, -18, 2, 17, -2, 7, -1, 4, -9, 13, 9, -11, 12, -6, -3, 6, -17, -1, 25, 15, -13, 3, 18, 21, 13, 15, -12, 5, 8, -12 };


	public static String encrypt(String input) {

		if (input == null) {
			return null;
		}

		byte[] outputBytes = input.getBytes();

		for (int i = 0; i < outputBytes.length; i++) {
			outputBytes[i] = (byte) (outputBytes[i] ^ seed[i % 34]);
		}

		BASE64Encoder encoder = new BASE64Encoder();
		String output = encoder.encode(outputBytes);

		return output;
	}


	public static String decrypt(String input) throws IOException {

		if (input == null) {
			return null;
		}

		BASE64Decoder decoder = new BASE64Decoder();
		byte[] byteBuffer = decoder.decodeBuffer(input);

		for (int i = 0; i < byteBuffer.length; i++) {
			byteBuffer[i] = (byte) (byteBuffer[i] ^ seed[i % 34]);
		}

		return new String(byteBuffer);

	}

}
